import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
import numpy as np
from sklearn import datasets
# 2.获取数据集
x_train=datasets.load_iris().data
y_train=datasets.load_iris().target

# 3.对数据集进行乱序
np.random.seed(112)
np.random.shuffle(x_train)
np.random.seed(112)
np.random.shuffle(y_train)
tf.random.set_seed(112)

# 建立模型
model=keras.Sequential()
# 添加一个全连接层
model.add(layers.Dense(3,activation='softmax',kernel_regularizer=keras.regularizers.l2()))

model.compile(
    optimizer=keras.optimizers.SGD(lr=0.1),
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=False),
    metrics=['sparse_categorical_accuracy']
)
model.fit(x_train,y_train,batch_size=32,epochs=500,validation_split=0.2,validation_freq=20)
model.summary()